 package server.life;
 import client.inventory.MapleInventoryType;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.fasterxml.jackson.core.JsonProcessingException;
import constants.ItemConstants;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import server.MapleItemInformationProvider;
import server.reward.RewardDropEntry;
import tools.Randomizer;

import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalTime;
import java.util.*;
























 public class MapleMonsterInformationProvider {
/*  27 */   private static final Logger log = LogManager.getLogger();
/*  28 */   private static final MapleMonsterInformationProvider instance = new MapleMonsterInformationProvider();
/*  29 */   private static final MapleDataProvider stringDataWZ = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz"));
/*  30 */   private static final MapleData mobStringData = stringDataWZ.getData("MonsterBook.img");
/*  31 */   private final Map<Integer, List<MonsterDropEntry>> drops = new TreeMap<>();
/*  32 */   private final List<MonsterGlobalDropEntry> globaldrops = new ArrayList<>();
/*  33 */   private final Map<Integer, Map<Integer, List<RewardDropEntry>>> specialdrops = new HashMap<>();

   public static MapleMonsterInformationProvider getInstance() {
/*  36 */     return instance;
   }

   public Map<Integer, List<MonsterDropEntry>> getAllDrop() {
/*  40 */     return this.drops;
   }

   public List<MonsterGlobalDropEntry> getGlobalDrop() {
/*  44 */     return this.globaldrops;
   }

   public Map<Integer, Map<Integer, List<RewardDropEntry>>> getFishDrop() {
/*  48 */     return this.specialdrops;
   }

   public void setDropData(int mobid, List<MonsterDropEntry> dropEntries) throws JsonProcessingException {
/*  52 */     setDropData(String.valueOf(mobid), dropEntries);
   }

   public void setDropData(String mobid, List<MonsterDropEntry> dropEntries) throws JsonProcessingException {
/*  56 */     this.drops.put(Integer.valueOf(mobid), dropEntries);
/*  57 */     update(Integer.valueOf(mobid).intValue(), dropEntries);
   }

   public void removeDropData(int mobid) {
/*  61 */     this.drops.remove(Integer.valueOf(mobid));
/*  62 */     update(mobid, null);
   }

   public void update(int mobid, List<MonsterDropEntry> dropEntries) {
/*  66 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/*  67 */         PreparedStatement ps = con.prepareStatement("DELETE FROM drop_data WHERE dropperid = ?")) {
/*  68 */       ps.setInt(1, mobid);
/*  69 */       ps.execute();
     }
/*  71 */     catch (SQLException sQLException) {}


/*  74 */     if (dropEntries != null) {
/*  75 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/*  76 */           PreparedStatement ps = con.prepareStatement("INSERT INTO drop_data VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?)")) {
/*  77 */         dropEntries.forEach(monsterDropEntry -> {
               try {
                 ps.setInt(1, mobid);
                 ps.setInt(2, monsterDropEntry.itemId);
                 ps.setInt(3, monsterDropEntry.minimum);
                 ps.setInt(4, monsterDropEntry.maximum);
                 ps.setInt(5, monsterDropEntry.questid);
                 ps.setInt(6, monsterDropEntry.chance);
                 ps.setString(7, MapleItemInformationProvider.getInstance().getName(monsterDropEntry.itemId));
                 ps.execute();
/*  87 */               } catch (SQLException e) {

                 e.printStackTrace();
               }
             });
/*  92 */       } catch (SQLException sQLException) {}
     }
   }


   public void load() {
     try {
/*  99 */       MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 100 */       List<Integer> mobids = new ArrayList<>();
/* 101 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 102 */           PreparedStatement ps = con.prepareStatement("SELECT dropperid FROM drop_data GROUP BY dropperid"); 
/* 103 */           ResultSet rs = ps.executeQuery()) {
/* 104 */         while (rs.next()) {
/* 105 */           mobids.add(Integer.valueOf(rs.getInt("dropperid")));
         }
       }



/* 111 */       mobids.forEach(mobid -> {
             List<MonsterDropEntry> list = new ArrayList<>();

             boolean exist = MapleLifeFactory.checkMonsterIsExist(mobid.intValue());

             if (!exist) {
               return;
             }

             MapleMonsterStats mons = MapleLifeFactory.getMonsterStats(mobid.intValue());

             if (mons == null) {
               return;
             }

             try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
               try (PreparedStatement ps = con.prepareStatement("SELECT * FROM drop_data WHERE dropperid = ?")) {
                 ps.setInt(1, mobid.intValue());

                 try (ResultSet rs = ps.executeQuery()) {
                   boolean doneMesos = false;

                   while (rs.next()) {
                     int itemid = rs.getInt("itemid");

                     int chance = rs.getInt("chance");

                     if (itemid != 0 && !ii.itemExists(itemid)) {
                       continue;
                     }

                     list = drops.computeIfAbsent(mobid, k -> new ArrayList<>());

                     Integer item = ii.getItemIdByMob(mobid.intValue());

                     if (item != null && item.intValue() > 0) {
                       list.add(new MonsterDropEntry(item.intValue(), mons.isBoss() ? 1000000 : 10000, 1, 1, 0));
                     }

                     list.add(new MonsterDropEntry(itemid, chance, rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getInt("questid")));

                     if (itemid == 0) {
                       doneMesos = true;
                     }
                   }
                   if (!doneMesos) {
                     addMeso(mons, list);
                   }
                 }
               }
               this.drops.put(mobid, list);
/* 162 */             } catch (SQLException e) {
               log.error("读取怪物爆率出错.", e);
             }
           });

/* 167 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 168 */           PreparedStatement ps = con.prepareStatement("SELECT * FROM drop_data_global"); 
/* 169 */           ResultSet rs = ps.executeQuery()) {
/* 170 */         while (rs.next()) {
/* 171 */           this.globaldrops.add(new MonsterGlobalDropEntry(rs
/* 172 */                 .getInt("itemid"), rs
/* 173 */                 .getInt("chance"), rs
/* 174 */                 .getInt("continent"), rs
/* 175 */                 .getByte("dropType"), rs
/* 176 */                 .getInt("minimum_quantity"), rs
/* 177 */                 .getInt("maximum_quantity"), rs
/* 178 */                 .getInt("questid")));
         }
       }




/* 185 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 186 */           PreparedStatement ps = con.prepareStatement("SELECT * FROM drop_data_special"); 
/* 187 */           ResultSet rs = ps.executeQuery()) {
/* 188 */         while (rs.next()) {
/* 189 */           int mapid = rs.getInt("mapid"), dropperid = rs.getInt("dropperid");
/* 190 */           Map<Integer, List<RewardDropEntry>> drop_data_special = this.specialdrops.computeIfAbsent(Integer.valueOf(mapid), k -> new HashMap<>());
/* 191 */           List<RewardDropEntry> list = drop_data_special.computeIfAbsent(Integer.valueOf(mapid), k -> new ArrayList());

/* 193 */           list.add(new RewardDropEntry(rs
/* 194 */                 .getInt("itemid"), rs
/* 195 */                 .getInt("chance"), rs
/* 196 */                 .getInt("quantity"), rs
/* 197 */                 .getInt("msgType"), rs
/* 198 */                 .getInt("period"), rs
/* 199 */                 .getInt("state")));
/* 200 */           drop_data_special.put(Integer.valueOf(dropperid), list);
/* 201 */           this.specialdrops.put(Integer.valueOf(mapid), drop_data_special);
         }
       }

/* 206 */       log.info("共加载 " + this.drops.size() + " 条爆率信息.");
/* 207 */     } catch (SQLException e) {
/* 208 */       log.error("导入怪物爆率错误", e);
     }
   }


   public List<MonsterDropEntry> retrieveDrop(int monsterId) {
/* 214 */     return this.drops.computeIfAbsent(Integer.valueOf(monsterId), k -> new ArrayList());
   }


   public List<RewardDropEntry> retrieveSpecialDrop(int mapid, int dropperId) {
/* 219 */     return ((Map<Integer, List<RewardDropEntry>>)this.specialdrops.computeIfAbsent(Integer.valueOf(mapid), k -> new HashMap<>())).computeIfAbsent(Integer.valueOf(dropperId), k -> new ArrayList());
   }

   public RewardDropEntry getReward(int mapid, int dropperId) {
/* 223 */     List<RewardDropEntry> dropEntry = retrieveSpecialDrop(0, dropperId);
/* 224 */     dropEntry.addAll(retrieveSpecialDrop(mapid, dropperId));
/* 225 */     int chance = (int)Math.floor(Math.random() * 1000.0D);
/* 226 */     List<RewardDropEntry> ret = new ArrayList<>();
/* 227 */     for (RewardDropEntry de : dropEntry) {
/* 228 */       if (de.chance >= chance) {
/* 229 */         ret.add(de);
       }
     }
/* 232 */     if (ret.isEmpty()) {
/* 233 */       return null;
     }
/* 235 */     Collections.shuffle(ret);
/* 236 */     return ret.get(Randomizer.nextInt(ret.size()));
   }

   public void addExtra() {
/* 240 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 241 */     for (Map.Entry<Integer, List<MonsterDropEntry>> e : this.drops.entrySet()) {
/* 242 */       for (int i = 0; i < ((List)e.getValue()).size(); i++) {
/* 243 */         if (((MonsterDropEntry)((List)e.getValue()).get(i)).itemId != 0 && !ii.itemExists(((MonsterDropEntry)((List)e.getValue()).get(i)).itemId)) {
/* 244 */           ((List)e.getValue()).remove(i);
         }
       }
/* 247 */       MapleMonsterStats mons = MapleLifeFactory.getMonsterStats(((Integer)e.getKey()).intValue());
/* 248 */       Integer item = ii.getItemIdByMob(((Integer)e.getKey()).intValue());
/* 249 */       if (item == null || item.intValue() <= 0 || 
/* 250 */         mons == null || item.intValue() / 10000 == 238) {
         continue;
       }
/* 253 */       ((List<MonsterDropEntry>)e.getValue()).add(new MonsterDropEntry(item.intValue(), mons.isBoss() ? 1000000 : 10000, 1, 1, 0));
     }
   }
































   public void addMeso(MapleMonsterStats mons, List<MonsterDropEntry> ret) {
/* 289 */     double divided = (mons.getLevel() < 100) ? ((mons.getLevel() < 10) ? mons.getLevel() : 10.0D) : (mons.getLevel() / 10.0D);
/* 290 */     int maxMeso = mons.getLevel() * (int)Math.ceil(mons.getLevel() / divided);
/* 291 */     if (mons.isBoss() && !mons.isPartyBonus()) {
/* 292 */       maxMeso *= 3;
     }
/* 294 */     for (int i = 0; i < mons.dropsMesoCount(); i++) {
/* 295 */       if (mons.getId() >= 9600086 && mons.getId() <= 9600098) {
/* 296 */         int meso = (int)Math.floor(Math.random() * 500.0D + 1000.0D);
/* 297 */         ret.add(new MonsterDropEntry(0, 20000, (int)Math.floor(0.46D * meso), meso, 0));
       } else {
/* 299 */         ret.add(new MonsterDropEntry(0, (mons.isBoss() && !mons.isPartyBonus()) ? 800000 : (mons.isPartyBonus() ? 600000 : 400000), (int)Math.floor(0.66D * maxMeso), maxMeso, 0));
       }
     }
   }

   public void clearDrops() {
/* 305 */     this.drops.clear();
/* 306 */     this.globaldrops.clear();
/* 307 */     this.specialdrops.clear();
/* 308 */     load();
/* 309 */     addExtra();
   }

   public boolean contains(List<MonsterDropEntry> e, int toAdd) {
/* 313 */     for (MonsterDropEntry f : e) {
/* 314 */       if (f.itemId == toAdd) {
/* 315 */         return true;
       }
     }
/* 318 */     return false;
   }














   public int chanceLogic(int itemId) {
/* 335 */     if (ItemConstants.getInventoryType(itemId) == MapleInventoryType.EQUIP)
/* 336 */       return 8000; 
/* 337 */     if (ItemConstants.getInventoryType(itemId) == MapleInventoryType.SETUP || ItemConstants.getInventoryType(itemId) == MapleInventoryType.CASH) {
/* 338 */       return 500;
     }
/* 340 */     switch (itemId / 10000) {
       case 204:
/* 342 */         return 1800;
       case 207:
       case 233:
/* 345 */         return 3000;
       case 229:
/* 347 */         return 400;
       case 401:
       case 402:
/* 350 */         return 5000;
       case 403:
/* 352 */         return 4000;
     }
/* 354 */     return 8000;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\life\MapleMonsterInformationProvider.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */